package com.aisb.manage;

import com.aisb.core.Result;
import com.aisb.core.util.DateUtil;
import com.aisb.core.util.file.ExcelUtils;
import com.aisb.core.log.LogOper;
import com.aisb.core.log.LogVisit;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/manage/log")
public class LogManageController {
    private static Logger logger = LoggerFactory.getLogger(LogManageController.class);

    @Autowired
    LogManageService logService;

    @RequestMapping(value = "/index")
    @LogVisit("日志管理列表")
    public String index(Model model) {
        return "manage/log";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result logList(@RequestParam Map param) {
        String pageNumStr = (String) param.get("pageNum");
        String pageSizeStr = (String) param.get("pageSize");

        Integer pageNum = Integer.parseInt(pageNumStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);

        Page page = logService.logList(param, pageNum, pageSize);

        Map r = new HashMap();
        r.put("total", page.getTotal());
        r.put("rows", page.getResult());

        return new Result(true, r);
    }

    @RequestMapping(value = "/export")
    @ResponseBody
    @LogOper("日志管理，数据导出")
    public Result userExport(@RequestParam Map param) {
        Page data = logService.logList(param, null, null);

        String fileName = "日志_" + DateUtil.getInstance().getToday("yyyyMMddHmmsss") + ".xls";
        boolean flag = ExcelUtils.getInstance().createExcel(fileName, data.getResult());

        return new Result(flag, fileName);
    }
}
